<template>
  <div class="home">
    <div class="main">
      <!-- 〠 -->
      <div><img src="../assets/img/icon_bilibili.png" alt="" /></div>
      <div class="count">
        <i
          class="iconfont icon-fensi"
          style="
            font-size: 40px;
            color: aliceblue;
            display: inline-block;
            padding: 0 10px;
          "
        ></i>
        <!-- 粉丝数量 -->
        <span class="playCount" ref="playCount">{{ fansCountNumber }}</span>
        <i class="playCount fansIsAdd" v-if="showFansAdd">{{
          `+${fansIsAdd}`
        }}</i>
        <i class="playCount fansIsDown" v-if="showFansDown">{{
          `-${fansIsAdd}`
        }}</i>
      </div>
    </div>
    <template v-if="videoTitle">
      <div class="title">
        <span class="videoTitle">{{ videoTitle }}</span>
        <span class="videoPlay"
          ><i class="iconfont icon-iconset0481" style="margin-right: 5px"></i
          >{{ videoInfo.view }}</span
        >
      </div>
      <div class="otherInfo">
        <div>
          <i class="iconfont icon-dianzan"></i>
          <span>{{ videoInfo.like }}</span>
        </div>
        <div>
          <i class="iconfont icon-Bbi"></i><span>{{ videoInfo.coin }}</span>
        </div>
        <div>
          <i class="iconfont icon-soucang"></i
          ><span>{{ videoInfo.favorite }}</span>
        </div>
        <div>
          <i class="iconfont icon-duanxin"></i
          ><span>{{ videoInfo.reply }}</span>
        </div>
        <div>
          <i class="iconfont icon-danmushu"></i
          ><span>{{ videoInfo.danmaku }}</span>
        </div>
        <div>
          <i class="iconfont icon-zhuanfa"></i
          ><span>{{ videoInfo.share }}</span>
        </div>
      </div>
    </template>
    <template v-else>
      <span style="color: #1295d9; display: inline-block; padding: 10px 0"
        >快前往设置配置数据源吧！</span
      >
    </template>
    <audio ref="audio" src="../assets/audio/bling.mp3" hidden></audio>
    <audio ref="audioFail" src="../assets/audio/fail.mp3" hidden></audio>
  </div>
</template>

<script>
import { CountUp } from "countup.js";
import emitter from "../utill/emitter";
import { getFansInfo } from "../request/userInfo/index";
import { getVideoInfo } from "../request/videoInfo/index";
const defaultOptions = {
  decimalPlaces: 0, // 保留两位
  duration: 1, // 动画时长
  separator: ",", // 千位分割
  decimal: ".", // 小数分割
  prefix: "", // 单位
};
let timer1 = null
let timer2 = null
export default {
  data() {
    return {
      fansCountNumber: 0,
      fansIsAdd: 0,
      showFansAdd: false,
      showFansDown: false,
      videoTitle: "",
      videoInfo: {},
    };
  },
  mounted() {
    emitter.on("refResh", () => {
      // 刷新全局数据
      this.getData();
    });
    if (this.$store.state.mid !== "") {
      this.getData();
      timer1=setInterval(() => {
        this.getData();
      }, 10000);
    }
    if (this.$store.state.videoUrl !== "") {
      this.getVideData();
      timer2=setInterval(() => {
        this.getVideData();
      }, 20000);
    }
    emitter.on("submit", () => {
      this.getData();
    });
  },
  methods: {
    getData() {
      getFansInfo(this.$store.state.mid).then((res) => {
        this.fansIsAdd = res.data.data.follower - this.fansCountNumber;
        if (this.fansIsAdd >= 0) {
          if (this.fansIsAdd > 0 && this.$store.state.isMute) {
              this.$refs.audio.play();
          }
          this.showFansAdd = true;
          setTimeout(() => {
            this.showFansAdd = false;
          }, 3000);
        } else {
            if(this.$store.state.isMute){
              this.$refs.audioFail.play();
            }
          this.showFansDown = true;
          setTimeout(() => {
            this.showFansDown = false;
          }, 3000);
        }
        this.fansCountNumber = res.data.data.follower;
        this.rollCountNumber();
      });
    },
    getVideData() {
      getVideoInfo(this.$store.state.videoUrl).then((res) => {
        this.videoTitle = res.data.data.title;
        this.videoInfo = res.data.data.stat;
      });
    },
    rollCountNumber() {
      const countUp = new CountUp(
        this.$refs.playCount,
        this.fansCountNumber,
        defaultOptions
      );
      countUp.start();
    },
  },
  destroyed() {
    clearInterval(timer1)
    clearInterval(timer2)
    timer1 = null
    timer2 = null
  },
};
</script>
<style scoped>
.home{
  height:215px;
}
.main {
  display: flex;
  justify-content: center;
  align-items: center;
  height:150px;
}
.count {
  position: relative;
}
.playCount {
  font-size: 50px;
  color: rgb(207, 207, 207);
  font-family: Impact, Haettenschweiler, "Arial Narrow Bold", sans-serif;
}
.title {
  font-size: 15px;
  color: rgb(207, 207, 207);
  display: flex;
  justify-content: center;
  align-items: center;
  font-family: fantasy;
  padding: 0 0 15px 0;
  margin-left: 50px;
}
.videoTitle {
  width: 250px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.videoPlay {
  display: inline-block;
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 60px;
  margin-left: 30px;
}
.otherInfo {
  display: flex;
  padding: 0 0 10px 0;
  justify-content: space-around;
  font-size: 15px;
  color: rgb(207, 207, 207);
  line-height: 20px;
}
.otherInfo div {
  display: flex;
  justify-content: space-between;
  width: 25px;
  cursor: pointer;
}
.fansIsAdd {
  position: absolute;
  z-index: 1;
  right: -30px;
  color: green;
  font-size: 25px;
}
.fansIsDown {
  position: absolute;
  z-index: 1;
  right: -30px;
  color: red;
  font-size: 25px;
}
</style>
